package com.ebt.m.customer.db;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CustomerDBHelper extends SQLiteOpenHelper{
	
	private static final String TAG = "wang";
	//数据库版本  
    private static final int VERSION = 17;  

    //新建一个表  
    String create_sql_customer_from_contact = "create table if not exists customer_from_contact"+  
    	    "(uuid varchar, customerName varchar, customerUuid varchar, contactName varchar, contactRawId varchar, contactSortKey varchar)";  
    
    String create_sql_customer = "create table if not exists customer"+  
    	    "(uuid varchar not null, name varchar, sortKeyString varchar, sortKeyChar varchar, importContact varchar, lastActiveTime INTEGER)";
    
    String create_sql_customer_contact = "CREATE TABLE customer_contact( uuid  VARCHAR2(100) PRIMARY KEY," +
    		" id  INTEGER, ctype  INTEGER NOT NULL DEFAULT 0, classification  INTEGER, cvalue  VARCHAR(100) NOT NULL," +
    		" customerId  INTEGER, customerUUId  VARCHAR2(100) NOT NULL, isDefault  INTEGER DEFAULT 0, description  VARCHAR(100)," +
    		" createTime  DATETIME DEFAULT (datetime()), lastUpdateTime  DATETIME, remark  VARCHAR(50)," +
    		" ChangeStatus INTEGER NOT NULL DEFAULT 0)";
    
    String create_sql_customer_address = "CREATE TABLE customer_address (uuid  VARCHAR2(100) PRIMARY KEY NOT NULL," +
    		" customerUUId  VARCHAR2(100) NOT NULL,	addressType  INTEGER, typeRemark varchar(50), countryCode varchar(20), " +
    		" province varchar(50), city  varchar(50), district varchar(50), details varchar(500), zipCode varchar(10)," +
    		" lastUpdateTime DATETIME, ChangeStatus INTEGER NOT NULL DEFAULT 0)";
    
    String drop_sql_customer_from_contact = "DROP TABLE IF EXISTS customer_from_contact";
    String drop_sql_customer = "DROP TABLE IF EXISTS customer";
    String drop_sql_customer_contact = "DROP TABLE IF EXISTS customer_contact";
    String drop_sql_customer_address = "DROP TABLE IF EXISTS customer_address";
    
	public CustomerDBHelper(Context context,String name){  
		this(context, name, VERSION);  
	}

	public CustomerDBHelper(Context context, String name, int version){  
		this(context, name, null, version);  
	}  
	      
	public CustomerDBHelper(Context context, String name, CursorFactory factory, int version) {  
        this(context, name, factory, version, null);  
    } 
	
	public CustomerDBHelper(Context context, String name, CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
		super(context, name, factory, version, errorHandler);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(create_sql_customer_from_contact);
		db.execSQL(create_sql_customer);
		db.execSQL(create_sql_customer_contact);
		db.execSQL(create_sql_customer_address);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d(TAG, "db onUpgrade");
		db.execSQL(drop_sql_customer_from_contact);
		db.execSQL(drop_sql_customer);
		db.execSQL(drop_sql_customer_contact);
		db.execSQL(drop_sql_customer_address);
		
		db.execSQL(create_sql_customer_from_contact);
		db.execSQL(create_sql_customer);
		db.execSQL(create_sql_customer_contact);
		db.execSQL(create_sql_customer_address);
	}
	
}
